var e = require("@vant/area-data"), t = d(require("@vant/weapp/toast/toast")), a = d(require("@vant/weapp/dialog/dialog"));

function d(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

var i = require("../../utils/request"), s = require("../../utils/api"), o = require("../../utils/check");

Page({
    data: {
        themeColor: getApp().globalData.themeColor,
        showSelectBirthdayPopup: !1,
        showSelectAddressPopup: !1,
        maxDate: new Date().getTime(),
        minDate: new Date(1900, 1, 1).getTime(),
        currentDate: new Date().getTime(),
        saveLoading: !1,
        deleteLoading: !1,
        areaList: e.areaList,
        addressId: "",
        recipientName: "",
        recipientMobile: "",
        addressShow: "",
        provinceCode: "",
        cityCode: "",
        districtCode: "",
        tag: "",
        postalCode: "",
        address: "",
        defaultValue: !1,
        addressStrShow: "",
        tagShow: !1,
        tagActions: [ {
            name: "家"
        }, {
            name: "公司"
        }, {
            name: "其他"
        } ]
    },
    onLoad: function(e) {
        var t = "";
        "" != e.provinceCode && null != e.provinceCode && (t += this.data.areaList.province_list[e.provinceCode] + " "), 
        "" != e.cityCode && null != e.cityCode && (t += this.data.areaList.city_list[e.cityCode] + " "), 
        "" != e.districtCode && null != e.districtCode && (t += this.data.areaList.county_list[e.districtCode]), 
        "{}" != JSON.stringify(e) && this.setData({
            addressId: e.addressId,
            recipientName: e.recipientName,
            recipientMobile: e.recipientMobile,
            provinceCode: e.provinceCode,
            cityCode: e.cityCode,
            districtCode: e.districtCode,
            addressStrShow: t,
            address: e.address,
            postalCode: e.postalCode,
            tag: e.tag,
            defaultValue: "true" == e.defaultValue
        });
    },
    onClickTag: function() {
        this.setData({
            tagShow: !0
        });
    },
    onTagSelect: function(e) {
        this.setData({
            tagShow: !1,
            tag: e.detail.name
        });
    },
    onTagClose: function() {
        this.setData({
            tagShow: !1
        });
    },
    onTagDelete: function() {
        this.setData({
            tag: ""
        });
    },
    onDefaultAddressChange: function(e) {
        this.setData({
            defaultValue: e.detail
        });
    },
    onClickSelectAddress: function() {
        this.setData({
            showSelectAddressPopup: !0
        });
    },
    onCloseSelectAddress: function() {
        this.setData({
            showSelectAddressPopup: !1
        });
    },
    confirmSelectAddress: function(e) {
        var a = "", d = e.detail.values, i = !0;
        d.forEach(function(e) {
            null != e && null != e.code && null != e.name && "" != e.code && "" != e.name ? a += e.name + " " : i = !1;
        }), i ? (this.setData({
            provinceCode: d[0].code,
            cityCode: d[1].code,
            districtCode: d[2].code
        }), this.setData({
            showSelectAddressPopup: !1,
            addressStrShow: a
        })) : (0, t.default)({
            message: "请选择完整的省市区",
            duration: 5e3
        }), this.setData({
            showSelectAddressPopup: !1,
            addressStrShow: a
        });
    },
    onSaveClick: function() {
        var e = this;
        "" != this.data.recipientName ? "" != this.data.recipientMobile ? o.isMobile(this.data.recipientMobile) ? "" != this.data.provinceCode && "" != this.data.cityCode && "" != this.data.districtCode ? "" != this.data.address ? (wx.showLoading({
            title: "正在保存...",
            mask: !0
        }), this.setData({
            saveLoading: !0
        }), "" == this.data.addressId || null == this.data.addressId ? i.request(i.HttpMethod.POST, s.apiRoute.wcaInsertMyAddress, {
            recipientName: this.data.recipientName,
            recipientMobile: this.data.recipientMobile,
            provinceCode: this.data.provinceCode,
            cityCode: this.data.cityCode,
            districtCode: this.data.districtCode,
            address: this.data.address,
            postalCode: this.data.postalCode,
            tag: this.data.tag,
            defaultValue: this.data.defaultValue
        }).then(function(a) {
            "20000" == a.code ? ((0, t.default)({
                message: "保存成功",
                duration: 5e3
            }), wx.hideLoading(), e.setData({
                saveLoading: !1
            }), wx.navigateBack({
                delta: 1
            })) : ((0, t.default)({
                message: "".concat(a.message),
                duration: 5e3
            }), wx.hideLoading(), e.setData({
                saveLoading: !1
            }));
        }) : i.request(i.HttpMethod.POST, s.apiRoute.wcaUpdateMyAddress, {
            addressId: this.data.addressId,
            recipientName: this.data.recipientName,
            recipientMobile: this.data.recipientMobile,
            provinceCode: this.data.provinceCode,
            cityCode: this.data.cityCode,
            districtCode: this.data.districtCode,
            address: this.data.address,
            postalCode: this.data.postalCode,
            tag: this.data.tag,
            defaultValue: this.data.defaultValue
        }).then(function(a) {
            "20000" == a.code ? ((0, t.default)({
                message: "保存成功",
                duration: 5e3
            }), wx.hideLoading(), e.setData({
                saveLoading: !1
            }), wx.navigateBack({
                delta: 1
            })) : ((0, t.default)({
                message: "".concat(a.message),
                duration: 5e3
            }), wx.hideLoading(), e.setData({
                saveLoading: !1
            }));
        })) : (0, t.default)({
            message: "请输入详细地址",
            duration: 5e3
        }) : (0, t.default)({
            message: "请选择完整的所在地区",
            duration: 5e3
        }) : (0, t.default)({
            message: "电话格式不正确",
            duration: 5e3
        }) : (0, t.default)({
            message: "请输入电话",
            duration: 5e3
        }) : (0, t.default)({
            message: "请输入姓名",
            duration: 5e3
        });
    },
    onDeleteClick: function() {
        var e = this;
        "" != this.data.addressId ? a.default.confirm({
            title: "警告",
            message: "您确定要删除吗？"
        }).then(function() {
            wx.showLoading({
                title: "正在删除...",
                mask: !0
            }), e.setData({
                deleteLoading: !0
            }), i.request(i.HttpMethod.POST, s.apiRoute.wcaDeleteMyAddress, {
                addressId: e.data.addressId
            }).then(function(a) {
                "20000" == a.code ? ((0, t.default)({
                    message: "删除成功",
                    duration: 5e3
                }), wx.hideLoading(), e.setData({
                    deleteLoading: !1
                }), wx.navigateBack({
                    delta: 1
                })) : ((0, t.default)({
                    message: "删除失败，".concat(a.message),
                    duration: 5e3
                }), wx.hideLoading(), e.setData({
                    deleteLoading: !1
                }));
            }).catch(function() {
                console.log("未删除");
            });
        }) : (0, t.default)({
            message: "该数据未保存无需删除",
            duration: 5e3
        });
    }
});